price <- read_csv("data/oil_price.csv")
## Rows: 911 Columns: 3
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (1): date
## dbl (2): cost, nominal
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(price)
## # A tibble: 6 x 3
## date cost nominal
## <chr> <dbl> <dbl>
## 1 1/1/1946 17.6 1.17
## 2 1/2/1946 17.7 1.17
## 3 1/3/1946 17.5 1.17
## 4 1/4/1946 18.9 1.27
## 5 1/5/1946 18.8 1.27
## 6 1/6/1946 18.6 1.27
# Convert the data frame to time series
price_ts <- price %>% dplyr::select(price = cost) %>% ts( start = c(1946, 1), frequency = 12)
dygraph(price_ts,
main = "World Monthly Oil price ",
ylab = "price in $",
xlab = "Months") %>%
dyRangeSelector()
ts_decompose(price_ts) # illustrating seasonality as well as other parameters
ts_cor(price_ts) # Illustrating the lag seasonality
acf(price_ts)
ts_seasonal(price_ts, type = "normal")
dy <- diff(price_ts) # taking the first difference of the time series to eliminate the trend and make it stationary
autoplot(dy)
fit<- snaive(dy) #Residual SD: 6.9603
print(summary(fit))
##
## Forecast method: Seasonal naive method
##
## Model Information:
## Call: snaive(y = dy)
##
## Residual sd: 6.9603
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.03463252 6.960302 3.816704 NaN Inf 1 0.1735167
##
## Forecasts:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Dec 2021 3.30 -5.6199864 12.219986 -10.341942 16.941942
## Jan 2022 3.67 -5.2499864 12.589986 -9.971942 17.311942
## Feb 2022 9.38 0.4600136 18.299986 -4.261942 23.021942
## Mar 2022 -2.85 -11.7699864 6.069986 -16.491942 10.791942
## Apr 2022 4.01 -4.9099864 12.929986 -9.631942 17.651942
## May 2022 2.28 -6.6399864 11.199986 -11.361942 15.921942
## Jun 2022 6.62 -2.2999864 15.539986 -7.021942 20.261942
## Jul 2022 0.12 -8.7999864 9.039986 -13.521942 13.761942
## Aug 2022 -5.61 -14.5299864 3.309986 -19.251942 8.031942
## Sep 2022 6.32 -2.5999864 15.239986 -7.321942 19.961942
## Oct 2022 8.54 -0.3799864 17.459986 -5.101942 22.181942
## Nov 2022 -1.64 -10.5599864 7.279986 -15.281942 12.001942
## Dec 2022 3.30 -9.3147658 15.914766 -15.992619 22.592619
## Jan 2023 3.67 -8.9447658 16.284766 -15.622619 22.962619
## Feb 2023 9.38 -3.2347658 21.994766 -9.912619 28.672619
## Mar 2023 -2.85 -15.4647658 9.764766 -22.142619 16.442619
## Apr 2023 4.01 -8.6047658 16.624766 -15.282619 23.302619
## May 2023 2.28 -10.3347658 14.894766 -17.012619 21.572619
## Jun 2023 6.62 -5.9947658 19.234766 -12.672619 25.912619
## Jul 2023 0.12 -12.4947658 12.734766 -19.172619 19.412619
## Aug 2023 -5.61 -18.2247658 7.004766 -24.902619 13.682619
## Sep 2023 6.32 -6.2947658 18.934766 -12.972619 25.612619
## Oct 2023 8.54 -4.0747658 21.154766 -10.752619 27.832619
## Nov 2023 -1.64 -14.2547658 10.974766 -20.932619 17.652619
checkresiduals(fit)
##
## Ljung-Box test
##
## data: Residuals from Seasonal naive method
## Q* = 298, df = 24, p-value < 2.2e-16
##
## Model df: 0. Total lags used: 24
fit_ets<- ets(price_ts) #Residual SD: 0.089
print(summary(fit_ets))
## ETS(M,A,N)
##
## Call:
## ets(y = price_ts)
##
## Smoothing parameters:
## alpha = 0.9999
## beta = 1e-04
##
## Initial states:
## l = 16.9783
## b = 0.3013
##
## sigma: 0.089
##
## AIC AICc BIC
## 8707.182 8707.248 8731.255
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set -0.2200578 4.788608 2.682685 -0.9218479 4.782429 0.2649497
## ACF1
## Training set 0.1964131
checkresiduals(fit_ets)
##
## Ljung-Box test
##
## data: Residuals from ETS(M,A,N)
## Q* = 39.488, df = 20, p-value = 0.005794
##
## Model df: 4. Total lags used: 24
fit_arima<- auto.arima(price_ts, d=1) #Residual SD: 4.682948
print(summary(fit_arima))
## Series: price_ts
## ARIMA(1,1,0)(0,0,2)[12]
##
## Coefficients:
## ar1 sma1 sma2
## 0.1985 -0.0685 -0.0557
## s.e. 0.0325 0.0332 0.0332
##
## sigma^2 estimated as 21.93: log likelihood=-2694.84
## AIC=5397.68 AICc=5397.73 BIC=5416.94
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.06079334 4.672897 2.56107 -0.1207549 4.404903 0.2529387
## ACF1
## Training set 0.0003898576
checkresiduals(fit_arima)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(1,1,0)(0,0,2)[12]
## Q* = 35.763, df = 21, p-value = 0.02324
##
## Model df: 3. Total lags used: 24
I choosed the ETS since it has the lowest residual
fcs <- forecast(fit_ets, h= 12*10-11)# forcasting 10 years
plot_forecast(fcs)
The prediction shows an increase in the oil price for the next 10 years. Which make sense since the oil is still going to be heavily used by lots of countries. Also, the oil use is going to change as well from electricity production to …..